package com.education.welco.controller.app;

import com.education.common.pojo.AjaxResult;
import com.education.common.pojo.TStudent;
import com.education.welco.service.ITStudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/app/scanCode")
@Api(tags = {"app扫码进入班级"})
public class AppScanCodeInClassController {

    @Autowired
    private ITStudentService studentService;

    /**
     * 学生扫码进班
     * 需要传递 姓名+手机号+班级id+学校id
     *
     * @param student 学生信息
     * @return
     */
    @GetMapping("/judgeStudent")
    @ApiOperation(value = "判断学生是否有归属班级", httpMethod = "GET")
    public AjaxResult judgeStudent(TStudent student) {
        TStudent tStudent;
        //从用户表查询信息判断人员类型
        tStudent = studentService.judgeStudent(student.getPhone());
        //学生表
        TStudent tStudent2 = studentService.judgeStudent2(student.getPhone());
        //申请进班状态 0审核中 1通过 2驳回
        if (null != tStudent2 && tStudent2.getApplicationStatus().equals("0")) {
            return AjaxResult.error("进班申请正在审批");
        }
        if (null != tStudent2 && tStudent2.getApplicationStatus().equals("1") && tStudent.getClassId().equals(student.getClassId())) {
            return AjaxResult.success("您已在当前班级,请勿重复申请");
        } else if (null != tStudent2 && tStudent2.getApplicationStatus().equals("1") && !tStudent.getClassId().equals(student.getClassId())) {
            return AjaxResult.error("您当前已有归属班级,请勿重复申请");
        }

        //驳回状态申请
        if (null != tStudent2 && tStudent2.getApplicationStatus().equals("2")) {
            //修改学生表申请入班数据
            student.setId(tStudent2.getId());
            student.setUserId(tStudent.getUserId());
            //申请进班状态 0审核中 1通过 2驳回
            student.setApplicationStatus("0");
            //班级id
            student.setClassId(student.getClassId());
            //学校id
            student.setSchoolId(student.getSchoolId());
            studentService.updateTStudent(student);
            return AjaxResult.success("进班申请已提交");
        } else {
            //新增学生[姓名、手机号、用户id(user表)、学生编号时间戳、身份证号、班级id、学校id]
            student.setUserId(tStudent.getUserId());
            //申请进班状态 0 审核中 1 通过 2 驳回
            student.setApplicationStatus("0");
            //学号
            long timeNew = System.currentTimeMillis() / 1000;
            student.setStudentCode(Long.toString(timeNew));
            //班级id
            student.setClassId(student.getClassId());
            //学校id
            student.setSchoolId(student.getSchoolId());
            studentService.insertTStudent(student);
            return AjaxResult.success("进班申请已提交");
        }
    }

}
